package com.jhws.sys.appUser.api;

import com.jhws.business.comm.bo.TblBuildingDimBo;
import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.business.comm.service.ITblCommDimService;
import com.jhws.business.device.bo.DeviceConstant;
import com.jhws.business.owner.bo.OwnerConstant;
import com.jhws.business.owner.bo.TblOwnerDimBo;
import com.jhws.sys.appUser.bo.TblSipAcountBindings;
import com.jhws.sys.appUser.service.ITblSipAcountDimService;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.business.device.bo.UserDevice;
import com.jhws.sys.appUser.bo.UserInfoV3;
import com.jhws.core.exception.BizException;
import com.jhws.business.comm.service.ITblBuildingDimService;
import com.jhws.util.ACLinkedMultiValueMap;
import com.jhws.util.AccountsUtils;
import com.jhws.util.StringUtils;
import com.jhws.util.commUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/2.
 */
@Controller
@RequestMapping(("/v3/appUser"))
public class appUserApiV3 extends appUserApiV2 {

    @Resource
    private ITblCommDimService ICommService;
    @Resource
    private ITblSipAcountDimService ISipAcountService;
    @Resource
    private ITblBuildingDimService IBuildingService;

    @Override
    /* 获取用户角色与角色相关楼栋信息，绑定设备列表 */
    public JsonRespData getUserInfoList(String username) throws Exception {

        // TODO Auto-generated method stub
        List<TblSipAcountBindings> list = ITblSipAcountDimService.getBindingsByUser(username);
        if (list == null || list.size() <= 0) {
            throw new BizException("-1", "getAllBindingList", "还没有绑定任何物业！");
        }

        // 是否保存过别墅类型，别墅只保存一条记录
        boolean isSaveVILLA_TYPE = false;
        String villaBinding = null;
        UserInfoV3 userInfoV3;
        List<UserInfoV3> uList = new ArrayList<>();
        ACLinkedMultiValueMap<String, UserDevice> acLinkedMultiValueMap = new ACLinkedMultiValueMap<String, UserDevice>();
        for (TblSipAcountBindings tblSipAcountBindings : list) {
            String sipNum = tblSipAcountBindings.getBinding();
            int devicesType = AccountsUtils.devicesType(sipNum);

            if (devicesType == DeviceConstant.WALL_TYPE) {
                String dispName = "围墙机-" + sipNum.substring(sipNum.length() - 1, sipNum.length());
                acLinkedMultiValueMap.add(sipNum.substring(0, 4), new UserDevice(sipNum, dispName));
            } else if (devicesType == DeviceConstant.OUTDOOR_TYPE) {
                String dispName = AccountsUtils.getUnitInfo(sipNum) + ":门口机-"
                        + sipNum.substring(sipNum.length() - 1, sipNum.length());
                acLinkedMultiValueMap.add(sipNum.substring(0, 4), new UserDevice(sipNum, dispName));
            } else if (devicesType == DeviceConstant.VILLA_TYPE) {
                if (!isSaveVILLA_TYPE) {
                    isSaveVILLA_TYPE = true;
                    villaBinding = tblSipAcountBindings.getBinding();
                    uList.add(
                            new UserInfoV3(2, villaBinding, "0000", "别墅", OwnerConstant.ROLE_VILLA_TYPE, 0));
                }
                acLinkedMultiValueMap.add(villaBinding,
                        new UserDevice(sipNum, "别墅机-" + sipNum.substring(sipNum.length() - 1, sipNum.length())));
            } else if (devicesType == DeviceConstant.INDOOR_TYPE) {
                String commCode = tblSipAcountBindings.getBinding().substring(0, 4);
                TblCommDimBo tblCommDimBo = ITblCommDimService.findTblCommDimById(commCode);
                TblOwnerDimBo tblOwnerDimBo = ITblOwnerDimService.findTblOwnerDimByMobOrRoomInfo(username,
                        tblSipAcountBindings.getBinding());
                if (tblOwnerDimBo != null) {
                    if (tblOwnerDimBo.getApplyType() == OwnerConstant.OWNER_TYPE || tblOwnerDimBo.getApplyType() == OwnerConstant.OWNER_TYPE_RENTER) {
                        userInfoV3 = new UserInfoV3(OwnerConstant.OWNER_TYPE, tblSipAcountBindings.getBinding(),
                                tblOwnerDimBo.getCommCode(), tblOwnerDimBo.getCommName(), OwnerConstant.ROLE_OWNER_TYPE, tblCommDimBo.getCommSoftVersion());
                    } else {
                        userInfoV3 = new UserInfoV3(tblOwnerDimBo.getApplyType(), tblSipAcountBindings.getBinding(),
                                tblOwnerDimBo.getCommCode(), tblOwnerDimBo.getCommName(), OwnerConstant.ROLE_FAMILY_TYPE, tblCommDimBo.getCommSoftVersion());
                    }

                    userInfoV3.setAreaAliasName(AccountsUtils.getAreaNameFromRoomInfo(tblOwnerDimBo.getRoomInfo()));

                    uList.add(userInfoV3);
                    List<TblSipAcountBindings> bList = ISipAcountService.genDefaultSipUnlockMonitorBindingList(username,
                            tblSipAcountBindings.getBinding());
                    for (TblSipAcountBindings binding : bList) {
                        String name = binding.getBinding();
                        if (AccountsUtils.devicesType(name) == DeviceConstant.WALL_TYPE)
                            name = commUtil.getGateDevAliasName(name, tblCommDimBo.getGateDevAliasName());
                        else if (AccountsUtils.devicesType(name) == DeviceConstant.OUTDOOR_TYPE)
                            name = "门口机-" + name.substring(name.length() - 1);
                        acLinkedMultiValueMap.add(userInfoV3.getBindingRoom(), new UserDevice(binding.getBinding(), name));
                    }

                }
            } else if (devicesType == DeviceConstant.MANAGE_TYPE) {
                String commCode = tblSipAcountBindings.getBinding().substring(0, 4);
                TblCommDimBo tblCommDimBo = ITblCommDimService.findTblCommDimById(commCode);
                if (tblCommDimBo != null) {
                    userInfoV3 = new UserInfoV3(0, tblSipAcountBindings.getBinding(), tblCommDimBo.getCommCode(),
                            tblCommDimBo.getCommName(), OwnerConstant.ROLE_MANAGER_TYPE, tblCommDimBo.getCommSoftVersion());
                    uList.add(userInfoV3);
                }
            }
        }

        for (UserInfoV3 uInfoV3 : uList) {
            if (acLinkedMultiValueMap.containsKey(uInfoV3.getBindingRoom())
                    && !uInfoV3.getUserType().equals(OwnerConstant.ROLE_MANAGER_TYPE)) {
                uInfoV3.setDevicesList(acLinkedMultiValueMap.getValues(uInfoV3.getBindingRoom()));
            } else if (uInfoV3.getUserType().equals(OwnerConstant.ROLE_MANAGER_TYPE)) {
                String where = "where 1=1 and comm_code = '" + uInfoV3.getCommCode() + "'";
                List<TblBuildingDimBo> bos = IBuildingService.findTblBuildingDimByWhere(where);
                List<TblCommDimBo> comms = ICommService.findTblCommDimByWhere(where);
                List<UserDevice> devicesList = new ArrayList<UserDevice>();
                UserDevice devices;
                for (TblBuildingDimBo tblBuildingDimBo : bos) {
                    String areaName = tblBuildingDimBo.getAreaName();
                    String buildingName = tblBuildingDimBo.getBuildingName();
                    String areaAliasName = tblBuildingDimBo.getAreaAliasName();
                    int devNumber = tblBuildingDimBo.getDevNumber();

                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(uInfoV3.getCommCode());
                    if (areaName.length() == 1) {
                        stringBuilder.append("0").append(areaName);
                    } else {
                        stringBuilder.append(areaName);
                    }

                    if (buildingName.length() == 1) {
                        stringBuilder.append("0").append(buildingName);
                    } else {
                        stringBuilder.append(buildingName);
                    }

                    for (int i = 0; i < devNumber; i++) {
                        String devNum = String.valueOf(i + 1);
                        String sipNum = stringBuilder.toString() + "000" + devNum;
                        if (StringUtils.isEmpty(areaAliasName)) {
                            devicesList.add(new UserDevice(sipNum, AccountsUtils.getUnitInfo(sipNum) + ":门口机-" + devNum));
                        } else {
                            devicesList.add(new UserDevice(sipNum, areaAliasName + AccountsUtils.getUnit(sipNum) + "单元:门口机-" + devNum));
                        }
                    }
                }

                int gateDevNum = comms.get(0).getGateDevNum();

                if (gateDevNum > 0) {
                    for (int i = 0; i < gateDevNum; i++) {
                        devices = new UserDevice();
                        String sipNum = uInfoV3.getCommCode() + "0000000" + (char) ('a' + i);
                        devices.setAccounts(sipNum);
                        devices.setDisplayName(commUtil.getGateDevAliasName(sipNum, comms.get(0).getGateDevAliasName()));
                        devicesList.add(devices);
                    }
                }
                uInfoV3.setDevicesList(devicesList);
            }
        }

		/*
         * userInfoList.setUnlockList(unlockList);
		 * userInfoList.setUserInfoList(uList);
		 */
        return new JsonRespData("getAllBindingList", 1, uList);
    }
}
